package kuangstudy;

/**
 * @author:李雄
 * @create:2022-07-05 20:59
 */
public class KuangRace implements Runnable {

    private static String winner;

    @Override
    public void run() {
        for (int i=0;i<=100;i++){
            if(Thread.currentThread().getName().equals("兔子") && i%10==0){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            boolean flag = gameOver(i);
            if (flag){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
        }
    }

    private boolean gameOver(int steps){
        if(winner !=null){//已经存在胜利者了
            return true;
        }{
            if(steps>=100){
                winner = Thread.currentThread().getName();
                System.out.println("winner is" + winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        KuangRace test1 = new KuangRace();
        Thread thread1 = new Thread(test1,"兔子");
        Thread thread2 = new Thread(test1,"乌龟");
        thread1.start();
        thread2.start();
    }
}
